﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using EstateNuke.Data.Properties;

namespace EstateNuke.Data
{
    public partial class Employee
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        public string DisplayName
        {
            get
            {
                return string.Format("{0}{1}", Name, IsDeleted ? " (已删除)" : "");
            }
        }

        /// <summary>
        /// </summary>
        public int CustomersCount
        {
            get
            {
                return InternalCustomers.Count(customer => customer.IsDeleted == false);
            }
        }

        /// <summary>
        /// </summary>
        public IList<Customer> Customers
        {
            get
            {
                return InternalCustomers.Where(customer => customer.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<Agreement> Agreements
        {
            get
            {
                return InternalAgreements.Where(agreement => agreement.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<SmallOrder> SmallOrders
        {
            get
            {
                return InternalAgreements.Where(agreement => agreement.IsDeleted == false).SelectMany(agreement => agreement.InternalSmallOrders).Where(smallOrder => smallOrder.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<LargeOrder> LargeOrders
        {
            get
            {
                return InternalAgreements.Where(agreement => agreement.IsDeleted == false).SelectMany(agreement => agreement.InternalLargeOrders).Where(largeOrder => largeOrder.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<Contract> Contracts
        {
            get
            {
                return InternalAgreements.Where(agreement => agreement.IsDeleted == false).SelectMany(agreement => agreement.InternalContracts).Where(contract => contract.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<Cancellation> Cancellations
        {
            get
            {
                return InternalAgreements.Where(agreement => agreement.IsDeleted == false).SelectMany(agreement => agreement.InternalCancellations).Where(cancellation => cancellation.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<LeaseAgreement> LeaseAgreements
        {
            get
            {
                return InternalLeaseAgreements.Where(leaseAgreement => leaseAgreement.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<LeaseOrder> LeaseOrders
        {
            get
            {
                return InternalLeaseAgreements.Where(leaseAgreement => leaseAgreement.IsDeleted == false).SelectMany(leaseAgreement => leaseAgreement.InternalLeaseOrders).Where(leaseOrder => leaseOrder.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<LeaseContract> LeaseContracts
        {
            get
            {
                return InternalLeaseAgreements.Where(leaseLeaseAgreement => leaseLeaseAgreement.IsDeleted == false).SelectMany(leaseLeaseAgreement => leaseLeaseAgreement.InternalLeaseContracts).Where(leaseContract => leaseContract.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<LeaseCancellation> LeaseCancellations
        {
            get
            {
                return InternalLeaseAgreements.Where(leaseLeaseAgreement => leaseLeaseAgreement.IsDeleted == false).SelectMany(leaseLeaseAgreement => leaseLeaseAgreement.InternalLeaseCancellations).Where(leaseCancellation => leaseCancellation.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<EmployeePlan> EmployeePlans
        {
            get
            {
                return InternalEmployeePlans.Where(employeePlan => employeePlan.IsDeleted == false).ToList();
            }
        }

        #endregion

        #region Add & Remove Customer

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public Customer AddCustomer(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var customer = new Customer
                               {
                                   RowId = Guid.NewGuid(),
                                   CreatedDate = DateTime.Now,
                               };
            InternalCustomers.Add(customer);
            customer.IsDeleted = false;
            customer.ModifiedDate = DateTime.Now;
            customer.ModifiedByUser = modifiedUser;
            return customer;
        }

        /// <summary>
        /// </summary>
        /// <param name = "customer"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveCustomer(Customer customer, User modifiedUser)
        {
            if (customer == null)
            {
                throw new ArgumentNullException("customer");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalCustomers.Contains(customer))
            {
                customer.Delete(modifiedUser);
            }
        }

        #endregion

        #region Add & Remove Agreement

        /// <summary>
        /// </summary>
        /// <returns></returns>
        internal Agreement AddAgreement(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var agreement = new Agreement
                                {
                                    RowId = Guid.NewGuid(),
                                    CreatedDate = DateTime.Now,
                                };
            InternalAgreements.Add(agreement);
            agreement.IsDeleted = false;
            agreement.ModifiedDate = DateTime.Now;
            agreement.ModifiedByUser = modifiedUser;
            agreement.SerialNumber = string.Format("SN{0}", DateTime.Now.ToString("yyyyMMddHHmmss"));
            agreement.AgreementStateId = 0;
            return agreement;
        }

        /// <summary>
        /// </summary>
        /// <param name = "agreement"></param>
        /// <param name = "modifiedUser"></param>
        internal void RemoveAgreement(Agreement agreement, User modifiedUser)
        {
            if (agreement == null)
            {
                throw new ArgumentNullException("agreement");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalAgreements.Contains(agreement))
            {
                agreement.Delete(modifiedUser);
            }
        }

        #endregion

        #region Add & Remove LeaseAgreement

        /// <summary>
        /// </summary>
        /// <returns></returns>
        internal LeaseAgreement AddLeaseAgreement(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var leaseAgreement = new LeaseAgreement
                                     {
                                         RowId = Guid.NewGuid(),
                                         CreatedDate = DateTime.Now,
                                     };
            InternalLeaseAgreements.Add(leaseAgreement);
            leaseAgreement.IsDeleted = false;
            leaseAgreement.ModifiedDate = DateTime.Now;
            leaseAgreement.ModifiedByUser = modifiedUser;
            leaseAgreement.SerialNumber = string.Format("SN{0}", DateTime.Now.ToString("yyyyMMddHHmmss"));
            leaseAgreement.LeaseAgreementStateId = 0;
            return leaseAgreement;
        }

        /// <summary>
        /// </summary>
        /// <param name = "leaseAgreement"></param>
        /// <param name = "modifiedUser"></param>
        internal void RemoveLeaseAgreement(LeaseAgreement leaseAgreement, User modifiedUser)
        {
            if (leaseAgreement == null)
            {
                throw new ArgumentNullException("leaseAgreement");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalLeaseAgreements.Contains(leaseAgreement))
            {
                leaseAgreement.Delete(modifiedUser);
            }
        }

        #endregion

        #region Add & Remove EmployeePlan

        /// <summary>
        /// </summary>
        /// <returns></returns>
        internal EmployeePlan AddEmployeePlan(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var employeePlan = new EmployeePlan
                                   {
                                       RowId = Guid.NewGuid(),
                                       CreatedDate = DateTime.Now,
                                   };
            InternalEmployeePlans.Add(employeePlan);
            employeePlan.IsDeleted = false;
            employeePlan.ModifiedDate = DateTime.Now;
            employeePlan.ModifiedByUser = modifiedUser;
            employeePlan.SaleArea = 0;
            employeePlan.LeaseArea = 0;
            employeePlan.TotalArea = 0;
            return employeePlan;
        }

        /// <summary>
        /// </summary>
        /// <param name = "employeePlan"></param>
        /// <param name = "modifiedUser"></param>
        internal void RemoveEmployeePlan(EmployeePlan employeePlan, User modifiedUser)
        {
            if (employeePlan == null)
            {
                throw new ArgumentNullException("employeePlan");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalEmployeePlans.Contains(employeePlan))
            {
                employeePlan.Delete(modifiedUser);
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            if (string.IsNullOrEmpty(Name))
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NotNullAllowed, RowId, "Employee", "Name"));
            }
            Name = Name.Length > 64 ? Name.Substring(0, 64) : Name;
            Gender = string.IsNullOrEmpty(Gender) ? null : Gender.Length > 1 ? Gender.Substring(0, 1) : Gender;
            QQ = string.IsNullOrEmpty(QQ) ? null : QQ.Length > 32 ? QQ.Substring(0, 32) : QQ;
            Phone = string.IsNullOrEmpty(Phone) ? null : Phone.Length > 32 ? Phone.Substring(0, 32) : Phone;
            Mobile = string.IsNullOrEmpty(Mobile) ? null : Mobile.Length > 32 ? Mobile.Substring(0, 32) : Mobile;
            Email = string.IsNullOrEmpty(Email) ? null : Email.Length > 256 ? Email.Substring(0, 256) : Email;
            Comment = string.IsNullOrEmpty(Comment) ? null : Comment.Length > 512 ? Comment.Substring(0, 512) : Comment;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<Customer>) Customers).ForEach(customer => customer.Delete(modifiedUser));
            ((List<Agreement>) Agreements).ForEach(agreement => agreement.Delete(modifiedUser));
            ((List<LeaseAgreement>) LeaseAgreements).ForEach(leaseAgreement => leaseAgreement.Delete(modifiedUser));
            ((List<EmployeePlan>) EmployeePlans).ForEach(employeePlan => employeePlan.Delete(modifiedUser));
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}